/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.base.entity.StoreSelfCare;
import com.chlitina.store.modules.base.entity.StoreSelfProduct;
import com.chlitina.store.modules.base.service.StoreCareService;
import com.chlitina.store.modules.base.service.StoreSelfCareService;
import com.chlitina.store.modules.expert.utils.ExpertUtils;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 店面自营护理Controller
 * @author liujiang
 * @version 2013-11-02
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeSelfCare")
public class StoreSelfCareController extends BaseController {

	@Autowired
	private StoreSelfCareService storeSelfCareService;
	@Autowired
	private StoreCareService storeCareService;
	
	@ModelAttribute
	public StoreSelfCare get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeSelfCareService.get(id);
		}else{
			return new StoreSelfCare();
		}
	}
	private String getSelectPage(Page page){
		Long select_storeSelfCare_id = (Long) CacheUtils.get("select_storeSelfCare_id");
		String currentId = "";
		if(select_storeSelfCare_id != null) {
	        int index = storeSelfCareService.getIndexById(select_storeSelfCare_id);
	        ExpertUtils.setPageNoByCurrentId(page, index);
	        currentId = String.valueOf(select_storeSelfCare_id);
	        CacheUtils.remove("select_storeSelfCare_id");
		}
		return currentId;
	}

	@RequestMapping(value = {"list", ""})
	public String list(StoreSelfCare storeSelfCare, HttpServletRequest request, HttpServletResponse response, Model model) {
		String careName = request.getParameter("careName");
		Page<StoreSelfCare> page = new Page<StoreSelfCare>(request, response);
		String currentId = getSelectPage(page);
        Page<StoreSelfCare> current_page = storeSelfCareService.find(page, careName); 
        model.addAttribute("page", current_page);
        model.addAttribute("careName", careName);
        model.addAttribute("currentId", currentId);
		return "modules/base/storeSelfCareList";
	}

	@RequestMapping(value = "form")
	public String form(StoreSelfCare storeSelfCare, Model model) {
		CacheUtils.put("select_storeSelfCare_id", storeSelfCare.getId());
		model.addAttribute("storeSelfCare", storeSelfCare);
		return "modules/base/storeSelfCareForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreSelfCare storeSelfCare, Model model, RedirectAttributes redirectAttributes) {
		Long id = storeSelfCare.getId();
		Store store = StoreUtils.getStore();
		storeSelfCare.setStore(store);
		storeSelfCare.setStatus(StoreSelfProduct.FLAG_ENABLE);
		if (!beanValidator(model, storeSelfCare)){
			return form(storeSelfCare, model);
		}
		storeSelfCareService.save(storeSelfCare);
		if(id == null) {
			StoreCare storeCare = new StoreCare();
			storeCare.setStore(store);
			storeCare.setCare_type(StoreCare.TYPE_SELF);
			storeCare.setSingle_price(storeSelfCare.getPrice()/storeSelfCare.getTimes());
			storeCare.setStoreSelfCare(storeSelfCare);
			storeCareService.save(storeCare);
		}
		addMessage(redirectAttributes, "保存店面自营护理'" + storeSelfCare.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeSelfCare/";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		storeSelfCareService.delete(id);
		addMessage(redirectAttributes, "删除店面自营护理成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeSelfCare/";
	}

}
